package top.lyc829.zzl_android.ui.profile.follow.base;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;

import java.util.List;

import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.databinding.FollowBaseFragmentBinding;
import top.lyc829.zzl_android.logic.bean.BaseBean;
import top.lyc829.zzl_android.logic.util.PageInfo;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.ui.base_detail.BaseDetailActivity;

public class FollowBaseFragment extends Fragment {

    private FollowBaseViewModel mViewModel;
    FollowBaseAdapter adapter;
    FollowBaseFragmentBinding binding;
    private int otherUserId = -1;

    public static FollowBaseFragment newInstance() {
        return new FollowBaseFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
//        return inflater.inflate(R.layout.follow_base_fragment, container, false);
        binding = FollowBaseFragmentBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(FollowBaseViewModel.class);

        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
        binding.followBaseRecyclerview.setLayoutManager(layoutManager);

        adapter = new FollowBaseAdapter();
        adapter.setAnimationEnable(true);   // recyclerview加载动画
        binding.followBaseRecyclerview.setAdapter(adapter);
        //adapter.setList(new BaseBean().getExamples());


//点击事件
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Intent intent=new Intent(getActivity(), BaseDetailActivity.class);
                intent.putExtra("id", ((BaseBean)adapter.getData().get(position)).getId());
                startActivity(intent);
            }
        });



        request();
        binding.swipeLayout.setRefreshing(true);
        refresh();
        initRefreshLayout();
        observe();
    }

//    private void initLoadMore() {
//        adapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void onLoadMore() {
//                request();
//            }
//        });
//        adapter.getLoadMoreModule().setAutoLoadMore(true);
//        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
//        adapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
//    }
    private void initRefreshLayout() {
        binding.swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        binding.swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
    }

    /**
     * 刷新
     */
    private void refresh() {
        // 这里的作用是防止下拉刷新的时候还可以上拉加载
        adapter.getLoadMoreModule().setEnableLoadMore(false);
        // 下拉刷新，需要重置页数
        request();
    }


    private void request() {
        if(otherUserId==-1) {
            mViewModel.getFollowBaseList(Utils.getUserId());
        }
        else
        {
            mViewModel.getFollowBaseList(otherUserId);
        }
    }


    //观察者
    private void observe()
    {
        mViewModel.getFollowBaseLiveData().observe(getViewLifecycleOwner(),baseBeans ->
        {
            binding.swipeLayout.setRefreshing(false);
   //        adapter.getLoadMoreModule().setEnableLoadMore(true);
            adapter.setList(baseBeans);
            adapter.getLoadMoreModule().loadMoreComplete();
        });

    }
    @Override
    public void onResume() {
        super.onResume();
        refresh();
    }
    public void setotherUserId(int otherUserId) {
        this.otherUserId = otherUserId;
    }
}
